package com.example.admin.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.admin.R;
import com.example.admin.SQLite.Complaint;
import com.example.admin.SQLite.HfBean;
import com.example.admin.SQLite.LunTan;
import com.example.admin.SQLite.ScBean;
import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.User;
import com.example.admin.SQLite.dao.ScBeanDao;
import com.example.admin.SQLite.dao.UserDao;
import com.example.admin.adapter.ForumHfdapter;
import com.example.admin.base.BaseActivity;
import com.example.admin.bean.PersonalBean;
import com.orhanobut.hawk.Hawk;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ForumDetailActivity extends BaseActivity {
    UserDao userDao;
    ScBeanDao scBeanDao;
    LunTan bean;
    TextView uName, tvTitle, tvCont;
    ImageView ivImage, uImg;
    TextView tvSc, TvTime;
    RecyclerView recyclerView;
    ForumHfdapter hAdapter;

    @Override
    public void setContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_forum_detail);
                userDao = new UserDao(this);
        scBeanDao = new ScBeanDao(this);
        recyclerView = findViewById(R.id.recyclerView);
        hAdapter = new ForumHfdapter();
        recyclerView.setAdapter(hAdapter);
        uName = findViewById(R.id.uName);
        uImg = findViewById(R.id.uImg);
        TvTime = findViewById(R.id.TvTime);
        tvTitle = findViewById(R.id.tvTitle);
        tvCont = findViewById(R.id.tvCont);
        ivImage = findViewById(R.id.ivImage);
        bean = (LunTan) getIntent().getSerializableExtra("bean");
        TvTime.setText(bean.luntanTime);
        ArrayList<User> list = userDao.findWhereUsers(bean.uid+"");
        if (list.size()>0){
            uName.setText(list.get(0).userName);
        }

     /*   if (Hawk.contains("PersonalBean" + bean.uid)) {
            PersonalBean beans = Hawk.get("PersonalBean" + bean.uid);
            uName.setText(beans.name);

        }*/
        if (Hawk.contains("user" + bean.uid)) {
            String imgPath = Hawk.get("user" + bean.uid);
            Glide.with(this).load(imgPath).into(uImg);
        }
        tvTitle.setText(bean.luntanTitle);
        tvCont.setText(bean.luntanContent);
        if (TextUtils.isEmpty(bean.luntanPic)) {
            ivImage.setVisibility(View.GONE);
        } else {
            Glide.with(this).load(bean.luntanPic).into(ivImage);
        }
        tvSc = findViewById(R.id.tvSc);
        getData();
        getDataHf();
        tvSc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User user = Hawk.get("user");
                ArrayList<ScBean> list = scBeanDao.FindWhereSC(bean.id + "", user.id + "");
                if (list.size() == 0) {
                    ScBean beans = new ScBean();
                    beans.ids = bean.id;
                    beans.uids = user.id;
                    long id = scBeanDao.addDataSc(beans);
                    getData();
                } else {
                    scBeanDao.DeleteSc(list.get(0).id);
                    getData();

                }
            }
        });
        findViewById(R.id.tvHf).setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("请输入回复");
            EditText input = new EditText(this);
            builder.setView(input);
            builder.setPositiveButton("回复", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String str = input.getText().toString().trim();
                    if (TextUtils.isEmpty(str)) {
                        return;
                    }
                    Date currentTime = new Date();
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String time = format.format(currentTime);
                    User user = Hawk.get("user");
                    String text = input.getText().toString();
                    HfBean bean2 = new HfBean();
                    bean2.ids = bean.id;
                    bean2.uids = user.id + "";
                    bean2.cont = text;
                    bean2.datas = time;
                    Long ll = scBeanDao.addDataHf(bean2);
                    Log.e(TAG, "onClick: " + ll);
                    getDataHf();
                }
            });

// 显示对话框

            builder.show();
        });

    }

    private void getDataHf() {
        ArrayList<HfBean> list = scBeanDao.FindWhereHf(bean.id + "");
        hAdapter.setNewData(list);
    }

    private void getData() {
        User user = Hawk.get("user");
        ArrayList<ScBean> list = scBeanDao.FindWhereSC(bean.id + "", user.id + "");
        Log.e(TAG, "getData: " + list.size());
        if (list.size() > 0) {
            tvSc.setText("已收藏");
        } else {
            tvSc.setText("收藏");
        }
    }

    @Override
    public void setInitView() {

    }
}